removing a case clause: bash expansion in sed regexp: X='a\.b' ; Y=';;' sed -n '/${X}/,/${Y}/d'

Posted by ChrisSM on Stack Overflow See other posts from Stack Overflow or by ChrisSM
Published on 2010-06-09T19:28:29Z Indexed on 2010/06/09 19:32 UTC
Read the original article Hit count: 174

Filed under:
|
|
|

I'm trying to remove a case clause from a bash script. The clause will vary, but will always have backslashes as part of the case-match string.

I was trying sed but could use awk or a perl one-liner within the bash script.

The target of the edit is straightforward, resembles:

 $cat t.sh
 case N in 
 a\.b); 
   #[..etc., varies] 
 ;;
 esac

I am running afoul of the variable expansion escaping backslashes, semicolons or both. If I 'eval' I strip my backslash escapes. If I don't, the semi-colons catch me up. So I tried subshell expansion within the sed. This fouls the interpreter as I've written it. More escaping the semi-colons doesn't seem to help.

X='a\.b' ; Y=';;'   
sed -i '/$(echo ${X} | sed -n 's/\\/\\\\/g')/,/$(echo ${Y} | sed -n s/\;/\\;/g')/d t.sh

And this:

perl -i.bak -ne 'print unless /${X}/ .. /{$Y}/' t.sh  # which empties t.sh

and

eval perl -i.bak -ne \'print unless /${X}/ .. /{$Y}/' t.sh  # which does nothing

© Stack Overflow or respective owner

Related posts about perl

Related posts about bash